/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(document).ready(function(){
    var first_cate = $("#first_cate").val();	
	$("#cut").click(function(){
	 var num=$("#num").val();
	  if($("#num").val()==1){
		return
	  }
	  else{
		  num--;
	  }
	  $("#num").val(num);
	  totalprice();
	})
	$("#add").click(function(){
		var num=$("#num").val();
		num++;
		$("#num").val(num);
		totalprice();
	})
	$('#num').bind('input propertychange', function() {
		var num=$("#num").val();
		if(num == '' ||num==0 ||isNaN(num)){
			if(first_cate == 1 ){
				$("#num").val(3);
			}
			else{
				$("#num").val(1);
			}
		}
	  totalprice();	  
	})
	function totalprice(){
		var num=$("#num").val();
		var price=Number($("#price").text());
		var install_price=Number($("#install_price").text());	
		if (first_cate==1){
			$("#total").text((price+install_price)*num);
		}
		else{
		$("#total").text(price*num);
		}
				
	}
    
    
    $('.add-to-cart').on('click',function(){
        if($(this).attr('data-havepackage')==0){
            $params = JSON.stringify({'goods_id':$(this).attr('data-goodsid'),'nums':$('#num').val()});
        }else{
            var goods_package_info = $('.package-selected').attr('data-id')+','+$('#num').val()+';';
            $params = JSON.stringify({'goods_id':$(this).attr('data-goodsid'),'nums':$('#num').val(),'goods_package_info':goods_package_info});
        }
        
        $add_cart_back = ajaxSynchroPostRequest("/api/cart/add",$params);
        console.log($add_cart_back);
        if($add_cart_back.success==1){
            layer.msg($add_cart_back.msg);
        }else{
            layer.msg($add_cart_back.msg);
        }
    });
    
    $('.buy-submit').on('click',function(){
        //var package_info = []
        var goods = [];
        if($(this).attr('data-havepackage')==0){
            goods.push([$(this).attr('data-goodsid'),$(this).attr('data-havepackage'),$('#num').val()]);
        }else{
            var goods_package_string = $('.package-selected').attr('data-id')+','+$('#num').val()+';';
            //package_info.push(goods_package_string)
            //var goods = [$(this).attr('data-goodsid'),$('#num').val(),package_info];
            goods.push([$(this).attr('data-goodsid'),$(this).attr('data-havepackage'),goods_package_string]);
        }
        
        
        $params = JSON.stringify({'goods_info':goods});
        console.log($params);
        $buy_back = ajaxSynchroPostRequest("/order/create/index",$params);
        console.log($buy_back);
        if($buy_back.success==1){
            layer.msg($buy_back.msg);
        }else{
            layer.msg($buy_back.msg);
        }
    });
    $('.package-item').on('click',function(){
        var that = $(this);
        $('.package-item').each(function(){
            $(this).removeClass('package-selected');
        });
        var price = that.attr('data-price');
        that.addClass('package-selected');
        $('#price').html(price);
        totalprice();
    });
    
});

